local hejiang = fk.CreateSkill{
  name = "hanqing__hejiang",
}

Fk:loadTranslationTable{
  ["hanqing__hejiang"] = "和将",
  [":hanqing__hejiang"] = "出牌阶段每种牌名限一次，你可以将一张基本牌置入一名角色的装备区（替换原装备），此牌离开装备区后，"..
  "你可以令你或失去者视为使用之。",

  ["#hanqing__hejiang"] = "和将：将一张基本牌置入一名角色的装备区（替换原装备）",
  ["#hanqing__hejiang-choose"] = "和将：你可以令一名角色视为使用【%arg】",
  ["#hanqing__hejiang-use"] = "和将：请视为使用【%arg】",
}

local mapper = {
  [Player.WeaponSlot] = "weapon",
  [Player.ArmorSlot] = "armor",
  [Player.OffensiveRideSlot] = "offensive_horse",
  [Player.DefensiveRideSlot] = "defensive_horse",
  [Player.TreasureSlot] = "treasure",
}

hejiang:addEffect("active", {
  anim_type = "support",
  prompt = "#hanqing__hejiang",
  card_num = 1,
  target_num = 1,
  interaction = UI.ComboBox {choices = { "WeaponSlot", "ArmorSlot", "OffensiveRideSlot", "DefensiveRideSlot", "TreasureSlot"} },
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).type == Card.TypeBasic and
      not table.contains(player:getTableMark("hanqing__hejiang-phase"), Fk:getCardById(to_select).trueName)
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and #selected_cards == 1 and
      #to_select:getAvailableEquipSlots(Util.convertSubtypeAndEquipSlot(self.interaction.data)) > 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local card = Fk:getCardById(effect.cards[1])
    room:addTableMark(player, "hanqing__hejiang-phase", card.trueName)
    local subtype = mapper[self.interaction.data]
    room:setCardMark(card, hejiang.name, {subtype, player.id})
    player:filterHandcards()
    room:moveCardIntoEquip(target, effect.cards, hejiang.name, true, player)
  end,
})

hejiang:addEffect("filter", {
  mute = true,
  card_filter = function(self, to_select, player, isJudgeEvent)
    return to_select:getMark(hejiang.name) ~= 0
  end,
  view_as = function(self, player, to_select)
    return Fk:cloneCard(to_select:getMark(hejiang.name)[1].."__hanqing__hejiang", to_select.suit, to_select.number)
  end,
})

hejiang:addEffect(fk.AfterCardsMove, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.toArea ~= Card.PlayerEquip then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and
            Fk:getCardById(info.cardId, true):getMark(hejiang.name) ~= 0 and
            Fk:getCardById(info.cardId, true):getMark(hejiang.name)[2] == player.id then
            return true
          end
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = {}
    for _, move in ipairs(data) do
      if move.toArea ~= Card.PlayerEquip then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerEquip and
            Fk:getCardById(info.cardId, true):getMark(hejiang.name) ~= 0 and
            Fk:getCardById(info.cardId, true):getMark(hejiang.name)[2] == player.id then
            room:setCardMark(Fk:getCardById(info.cardId, true), hejiang.name, 0)
            if move.from then
              dat[move.from] = dat[move.from] or {}
              table.insertIfNeed(dat[move.from], info.cardId)
            end
          end
        end
      end
    end
    if next(dat) then
      for from, cards in pairs(dat) do
        for _, id in ipairs(cards) do
          if not player:hasSkill(hejiang.name) then return end
          local card = Fk:cloneCard(Fk:getCardById(id).name)
          if card.type ~= Card.TypeEquip and card.sub_type ~= Card.SubtypeDelayedTrick then
            local targets = {}
            for _, p in ipairs({player, from}) do
              if p:canUse(card, { bypass_times = true}) then
                table.insertIfNeed(targets, p)
              end
            end
            if #targets > 0 then
              local to = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = hejiang.name,
                prompt = "#hanqing__hejiang-choose:::"..card.name,
                cancelable = true,
              })
              if #to > 0 then
                to = to[1]
                room:askToUseVirtualCard(to, {
                  name = card.name,
                  skill_name = hejiang.name,
                  prompt = "#hanqing__hejiang-use:::"..card.name,
                  cancelable = false,
                  extra_data = {
                    bypass_times = true,
                    extraUse = true,
                  },
                })
              end
            end
          end
        end
      end
    end
  end,
})

return hejiang
